package org.adream.account.service;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.adream.account.dao.GradeDao;
import org.adream.account.entity.GradeEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.util.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 
 * @author sl
 *
 */
@Service
public class GradeService {
	
	@Autowired
	private GradeDao gradeDao;
	
	@Autowired  
	private HttpSession session;
	
	private final static Logger logger = LoggerFactory.getLogger(GradeService.class);

	/**
	 * 增
	 * @param grade
	 */
	public ResultModel<String> addGrade(GradeEntity grade) {
		String operator = (String) session.getAttribute("uid");
		grade.setCreator(operator);
		grade.setModifier(operator);
		grade.setDr(Constant.ONE);
		int result = gradeDao.addGrade(grade);
		if(result == 0) {
			logger.warn("新增职称/级别数据失败,数据库错误,gname:{}", grade.getGname());
			return new ResultModel<String>(ResultModel.ERR_SYS, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 删除，若有子级,直接删除  逻辑删除
	 * @param gid
	 * @return
	 * @throws ADreamServiceException
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = ADreamServiceException.class, isolation = Isolation.DEFAULT)
	public ResultModel<String> deleteGrade(Integer gid) throws ADreamServiceException {
		GradeEntity grade = gradeDao.queryGradeByGidAndDr(gid, Constant.ONE);
		if(grade == null) {
			logger.warn("删除职称/级别失败,数据不存在,gid:{}", gid);
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "删除失败,数据不存在", null);
		}
		String modifier = (String)session.getAttribute("uid");
		grade.setModifier(modifier);
		grade.setDr(Constant.ZERO);
		int result = gradeDao.deleteGrade(grade);
		if(result == 0) {
			logger.warn("删除职称/级别数据失败,数据库错误,gid:{}", gid);
			return new ResultModel<String>(ResultModel.ERR_SYS, "系统错误,请联系系统管理员", null);
		}
		//查询是否有子职称/级别
		List<GradeEntity> grades = gradeDao.queryGradesByPidAndDr(gid, Constant.ONE);
		int gradesSize = grades.size();
		for (int i = 0; i < gradesSize; i++) {
			GradeEntity sonGrade = grades.get(i);
			sonGrade.setDr(Constant.ZERO);
			sonGrade.setModifier(modifier);
			int sonResult = gradeDao.deleteGrade(sonGrade);
			if(sonResult == 0) {
				throw new ADreamServiceException("根据父职称/级别删除失败");
			}
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 更新
	 * @param gid
	 * @param gradeEntity
	 * @return
	 */
	public ResultModel<String> updateGrade(Integer gid,GradeEntity gradeEntity) {
		GradeEntity grade = gradeDao.queryGradeByGidAndDr(gid, Constant.ONE);
		if(grade == null) {
			logger.warn("更新职称/级别失败,数据不存在,gid:{}", gid);
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "更新失败,数据不存在", null);
		}
		String modifier = (String) session.getAttribute("uid");
		grade.setGname(gradeEntity.getGname());
		grade.setPid(gradeEntity.getPid());
		grade.setModifier(modifier);
		int result = gradeDao.updateGrade(grade);
		if(result == 0) {
			logger.warn("更新职称/级别数据失败,数据库错误,gid:{}", gid);
			return new ResultModel<String>(ResultModel.ERR_SYS, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 根据gid查询grade
	 * @param gid
	 * @return
	 */
	public GradeEntity queryGradeByGid(Integer gid) {
		return gradeDao.queryGradeByGid(gid);
	}
	
	/**
	 * 根据删除标志查询grade
	 * @param dr 删除标志
	 * @return
	 */
	public List<GradeEntity> queryGradesByDr(Integer dr) {
		List<GradeEntity> grades = gradeDao.queryGradesByDr(dr);
		if(grades != null && grades.size() > Constant.ZERO) {
			grades.forEach(o -> {
				if(o.getPid() != Constant.ZERO) {
					GradeEntity pGrade = gradeDao.queryGradeByGid(o.getPid());
					o.setpGrade(pGrade);
				}
			});
		}
		return grades;
	}
	
	/**
	 * 根据父级id,删除标志查询grade
	 * @param pid  父级id
	 * @param dr   删除标志
	 * @return
	 */
	public List<GradeEntity> queryGradesByPidAndDr(Integer pid,Integer dr) {
		return gradeDao.queryGradesByPidAndDr(pid, dr);
	}
	
	/**
	 * 根据删除标志，父级id,gname查询grade
	 *   查询条件拼接:
	 *     dr不能为null，pid,gname可为null
	 * @param dr  删除标志
	 * @param pid 父级id
	 * @param gname
	 * @param pageNo   当前页数
	 * @param pageSize 每页展示数目
	 * @return
	 * @throws ADreamServiceException
	 */
	public PageInfo<GradeEntity> queryGradesBySomeFields(Integer dr,Integer pid,String gname,Integer pageNo,Integer pageSize) {
		pageNo = (pageNo == null) ? Constant.ONE : pageNo;
		pageSize = (pageSize == null) ? Constant.TEN : pageSize;
		PageHelper.startPage(pageNo,pageSize);
		List<GradeEntity> grades = gradeDao.queryGradesBySomeFields(dr, pid, gname);
		if(grades != null && grades.size() > Constant.ZERO) {
			grades.forEach(o -> {
				if(o.getPid() != Constant.ZERO) {
					GradeEntity pGrade = gradeDao.queryGradeByGid(o.getPid());
					o.setpGrade(pGrade);
				}
			});
		}
		PageInfo<GradeEntity> page = new PageInfo<>(grades,Constant.TEN);
		return page;
	}
	
	/**
	 * 检查gname是否已存在
	 * @param gname
	 * @return
	 */
	public boolean isValidGname(String gname) {
		logger.info("检查grade gname是否已存在");
		boolean isValidGname = false;
		GradeEntity gradeEntity = gradeDao.queryGradeByGname(gname);
		if(!StringUtils.isEmpty(gname) && gradeEntity == null) {
			isValidGname = true;
		}
		return isValidGname;
	}
}